package com.example.demo.util;

import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;

public class ParameterValidator {
    public JSONObject validate(String content) {
        JSONObject result = null;
        JSONObject object = null;
        try {
            object = JSONObject.fromObject(content);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("errorCode","1001");
        }

        if(!object.containsKey("fpcontent")){
            result.put("errorCode","1006");
        }else {
            String dd = object.getString("fpcontent");
            String base64Decoded = null;
            try {
                base64Decoded = new String(Base64.decodeBase64(dd), "utf-8");
            }catch (Throwable e){
                result.put("errorCode","1001");
//            throw new Exception("fpcontent字段不能被Base64解码");
            }


            try {
                result = JSONObject.fromObject(base64Decoded);
                result.put("errorCode","0000");
                if(!result.containsKey("type") || !result.containsKey("value")){
                    result.put("errorCode","1003");
                }
            }catch (Throwable throwable){
                result.put("errorCode","1009");
//            throw new Exception("fpcontent格式不是合法的JSON结构");
            }
        }

        if(!object.containsKey("ZFQQLSH")){
            result.put("errorCode","1007");
        }

        if(!object.containsKey("callbackURL")){
            result.put("errorCode","1008");
        }
        return result;

//        String sample = FileUtils.readFileToString(Validator.getResourceFile("content.json"),"utf-8");
//        JSONObject obj = JSONObject.fromObject(sample);
    }
}
